﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.DirectoryServices;
using System.IO;
using System.Management;
using System.Security;
using Microsoft.Web.Administration;
using Microsoft.Web.Management;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
   
    public partial class Form1 : Form
    {

        delegate void SetTextCallback(string text);

        public Form1()
        {
            InitializeComponent();
            Servers.DataSource = wmi2.Datapod();
            Username.Text = System.Windows.Forms.SystemInformation.UserName.ToString();
            
        }
       
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        //Ask for Server!
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Needs this be sent throughout the application 
           
            string FullComputerName = Servers.Text;
            string Name = Username.Text;
            string PW = Password.Text;
          
           
        }

        //Take Username
        private void Username_TextChanged(object sender, EventArgs e)
        {
           
        }
   
        //Take password
        private void Password_TextChanged(object sender, EventArgs e)
        {
            string PasswordCode = Password.Text;

        }    

        //Turns Server offline 
        private void ServerOffline_Click(object sender, EventArgs e)
        {
           wmi2.ServerOffline(Servers.Text);
        }

        //Turns Server online 
        private void ServerOnline_Click(object sender, EventArgs e)
        {
          wmi2.ServerOnline(Servers.Text);
        }

        //Reset IIS
        private void ISS_RESET_Click(object sender, EventArgs e)
        {
          wmi2.IISReset(Servers.Text);
        }

        //Export Registry 
        private void Export_Registry_Click(object sender, EventArgs e)
        {
          //wmi2.ExportReg("HKEY_LOCAL_MACHINE\\Software\\RCI", "C:\\myLocal.reg", Servers.Text);
        }

        //Appends the Text, after enter button is click. 
        private void CountersForForm_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.RunWorkerAsync();

        }

        //Writes the  status of that specfic server. 
        private void Status_Button_Server_Click(object sender, EventArgs e)
        {
          
            string StateS = wmi2.CheckPool(Servers.Text);


            if (StateS == "OK")
            {
                Servername_box.ResetText();
                Servername_box.AppendText(Servers.Text);
                Server_Health.ResetText();
                Server_Health.AppendText(wmi2.CheckPool(Servers.Text));
                Server_State.ResetText();
                Server_State.AppendText("Online");
                AttentionChecker.ResetText();
                AttentionChecker.AppendText(wmi2.CheckAttenion(Servers.Text));
            }
          else if (StateS == "NOT OK")
            {
                Servername_box.ResetText();
                Servername_box.AppendText(Servers.Text);
                Server_Health.ResetText();
                Server_Health.AppendText(wmi2.CheckPool(Servers.Text));
                Server_State.ResetText();
                Server_State.AppendText("Online");
                AttentionChecker.ResetText();
                AttentionChecker.AppendText(wmi2.CheckAttenion(Servers.Text));
            }

          else 
            {
              
                MessageBox.Show((Servers.Text));
                Servername_box.ResetText();
                Servername_box.AppendText(Servers.Text);
                Server_Health.ResetText();
                Server_Health.AppendText("Not reachable");
                Server_State.ResetText();
                Server_State.AppendText("Not Reachable");
                AttentionChecker.ResetText();
                AttentionChecker.AppendText(wmi2.CheckAttenion(Servers.Text));


            }
        }

        //Added Cross threading to speed up process and added a background worker 
        private void ThreadProcSafe()
        {
            this.SetText(Servers.Text);
        }

        //Added Cross threading to speed up process and added a background worker
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the 
            // calling thread to the thread ID of the creating thread. 
            // If these threads are different, it returns true. 
            if (this.Servers.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.Servers.Text = text;
            }
        }

        //Added Cross threading to speed up process and added a background worker
        //Where calls are being made for counters. 
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
               string FullComputerName = Servers.Text;
                string Name = Username.Text;
                string PW = Password.Text;


                //Loops through Counters, but it takes for ever to go through each call. 
                for (int i = 0; i < 1; i++)
                {
                    ARTEXT.ResetText();
                    ARTEXT.AppendText(wmi2.PerformanceMonitor(FullComputerName, Name, PW, "1"));
                    ARTEXTSEC.ResetText();
                    ARTEXTSEC.AppendText(wmi2.PerformanceMonitor(FullComputerName, Name, PW, "2"));
                    REQWT.ResetText();
                    REQWT.AppendText((wmi2.PerformanceMonitor(FullComputerName, Name, PW, "3")));
                    REQEXC.ResetText();
                    REQEXC.AppendText(wmi2.PerformanceMonitor(FullComputerName, Name, PW, "4"));
                    SSA.Clear();
                    SSA.AppendText(wmi2.PerformanceMonitor(FullComputerName, Name, PW, "5"));
                    AVGDL.ResetText();
                    AVGDL.AppendText(wmi2.PerformanceMonitor(FullComputerName, Name, PW, "6"));

                }
            
           
        }


    }
}
